package com.qf.service.impl;

import com.qf.dao.FlowerMapper;
import com.qf.dao.StoreLastMapper;
import com.qf.pojo.entity.Goods;
import com.qf.pojo.entity.StoreLast;
import com.qf.pojo.req.ClassifyListReq;
import com.qf.pojo.resp.BasePerResp;
import com.qf.pojo.resp.BaseResp;
import com.qf.pojo.vo.GoodsClassifyVo;
import com.qf.pojo.vo.StoreInfoVo;
import com.qf.service.FlowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class FlowerServiceImpl implements FlowerService {

    @Autowired
    FlowerMapper flowerMapper;

    @Autowired
    StoreLastMapper storeLastMapper;

    @Override
    public BaseResp findAll(ClassifyListReq classifyListReq) {
        List<StoreInfoVo> allFlower = flowerMapper.findAllFlower(classifyListReq.getId());


        List<StoreLast> allLast = storeLastMapper.findAllLast(classifyListReq.getId());
        BasePerResp basePerResp = new BasePerResp();
        basePerResp.setStoreInfos(allFlower);
        basePerResp.setLast(allLast);
        if (basePerResp!=null) {
            return  new BaseResp(200,"success",basePerResp);
        }


        return new BaseResp().ERROR();
    }

    @Override
    public BaseResp findClassify(Integer id) {
        List<GoodsClassifyVo> classify = flowerMapper.findClassify(id);
        if (classify != null) {
            return new BaseResp(200,"success",classify);
        }
        return new BaseResp().ERROR();
    }

    @Override
    public BaseResp findGoodsByClassifyId(Integer id) {
        List<Goods> goodsByClassifyId = flowerMapper.findGoodsByClassifyId(id);
        if (goodsByClassifyId != null) {
            return new BaseResp(200, "success", goodsByClassifyId);
        }
        return new BaseResp().ERROR();
    }

    @Override
    public BaseResp findByFlowerName(String flowerName) {
        List<Goods> byFlowerName = flowerMapper.findByFlowerName(flowerName);
        if (byFlowerName != null) {
            return new BaseResp(200, "success", byFlowerName);
        }

        return new BaseResp().ERROR();
    }

    @Override
    public BaseResp findByFree(String freeName) {
        List<StoreInfoVo> allFlower = flowerMapper.findAllFlower(3);
       List<StoreInfoVo> list = new ArrayList<StoreInfoVo>();

        for (StoreInfoVo storeInfoVo : allFlower) {
            if (storeInfoVo.getStoreDiscount().equals(freeName)){
                    list.add(storeInfoVo);
            }
        }

        if (list!=null){
            //System.out.println(list);
            return new BaseResp(200, "success", list);
        }

        return new BaseResp().ERROR();
    }
}
